package com.kgc.ljp.server.dao.imp;

import com.kgc.ljp.server.dao.UserPlayDao;
import com.kgc.ljp.server.entity.*;
import com.kgc.ljp.server.util.Init;
import com.kgc.ljp.server.util.SetUtil;
import java.util.*;

public class UserPlayDaoImp implements UserPlayDao {
    //全局随即对象
    Random random  = new Random();
    static Map<String,User> userMap = new HashMap<>();
    static Map<String,Bag> bagMap = new HashMap<>();
    static Map<String,HeroClass> heroClassMap = new HashMap<>();
    static Map<String,KunClass> kunClassMap = new HashMap<>();
    static Map<String,JuQingPoint> juQingPointMap = new HashMap<>();
    static WuJingPoint wujin = null;
    static HeroClass hc = null;
    /**
     * 英雄属性
     */
    static int hBlood ;
    static int hDefense ;
    static int hPower ;
    static int hBoom ;
    static String hName;
    static String hSkill;
    /**
     * 静态初始化代码块
     */
    static {
        wujin = Init.wujin;
        userMap = (Map<String, User>) SetUtil.readObj("User.txt");
        if(userMap==null){
            userMap = Init.userMap;
        }
        bagMap = (Map<String, Bag>) SetUtil.readObj("Bag.txt");
        if (bagMap==null){
            bagMap = Init.bagMap;
        }
        heroClassMap = (Map<String, HeroClass>) SetUtil.readObj("HeroClass.txt");
        if (heroClassMap==null){
            heroClassMap = Init.heroClassMap;
        }
        kunClassMap = (Map<String, KunClass>) SetUtil.readObj("KunClass.txt");
        if (kunClassMap==null){
            kunClassMap = Init.kunClassMap;
        }
        juQingPointMap = (Map<String, JuQingPoint>) SetUtil.readObj("JuQingMap.txt");
        if (juQingPointMap==null){
            juQingPointMap = Init.juQingPointMap;
        }
        SetUtil.writeObj(userMap,"User.txt");
        SetUtil.writeObj(bagMap,"Bag.txt");
        SetUtil.writeObj(heroClassMap,"HeroClass.txt");
        SetUtil.writeObj(kunClassMap,"KunClass.txt");
        SetUtil.writeObj(juQingPointMap,"JuQingMap.txt");
    }

    /**
     * 注册类
     * @param user
     * @return
     */
    @Override
    public boolean regiest(User user) {
        userMap = (Map<String, User>) SetUtil.readObj("User.txt");
        if (userMap==null){
            userMap = Init.userMap;
        }
        if (!userMap.containsKey(user.getName())){
            List<HeroClass> heroClassList = new ArrayList<>();
            heroClassList.add(heroClassMap.get("C级叫花鸡"));
            Bag bag = new Bag(user.getName(),heroClassList);
           userMap.put(user.getName(),user);
           bagMap.put(user.getName(),bag);
           SetUtil.writeObj(userMap,"User.txt");
           SetUtil.writeObj(bagMap,"Bag.txt");
           return true;
        }else {
            return false;
        }
    }

    /**
     * 无尽游戏类
     */
    @Override
    public int playWuJinGame(HeroClass hc,List<KunClass> kunList,User user) {
        int floor = 0;
        this.hc = hc;
        hDefense = user.getDefense();
        hBlood = user.getBlood();
        hPower = user.getPower();
        hBoom = user.getBoom();
        hName = user.getName();
        hSkill = this.hc.getSkill();
        for (KunClass kc: kunList) {
            if (hBlood<=5){
                break;
            }
            floor++;
            playGanme(kc,hc,user);
            try {
                System.out.println("\n\n稍作休息，下拨即将来临");
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("结束，当前关卡守护者："+kunList.get(floor-2).getName());
        user.setEmprical(user.getEmprical()+10*(floor-2));
        SetUtil.writeObj(userMap,"User.txt");
        return floor;
    }

    /**
     * 返回无尽关卡
     */
    public WuJingPoint selectWuJin(){
        return wujin;
    }
    /**
     * 剧情
     */
    @Override
    public boolean playJuQingGame(HeroClass hc,KunClass kc,User user) {
        System.out.println("敌方数据");
        kc.print();
        /**
         * 英雄属性
         */
        hDefense = user.getDefense();
        hBlood = user.getBlood();
        hPower = user.getPower();
        hBoom = user.getBoom();
        hName = user.getName();
        hSkill = hc.getSkill();
        return playGanme(kc,hc,user);
    }
    public boolean playGanme(KunClass kc,HeroClass hc,User user){
            //用于提醒释放技能
            int i = 0;
            //判断是否触发暴击
            Boolean boom = false;
            //储存暴击数据
            int numes = 0;
            /**
             * 鲲属性
             */
            int kBlood = kc.getBlood();
            int kDefense = kc.getDefense();
            int kPower = kc.getPower();
            String kName = kc.getName();
            String kSkill = hc.getSkill();
            /**
             * 伤害计算公式
             */
            //未暴击
            int heroNo = hPower-kDefense*2/10;
            //暴击
            int heroYes = hPower*150/100-kDefense*2/10;
            //未暴击
            int kunNo = kPower-hDefense*2/10;
            //技能
            int kunYes = kPower*150/100-hDefense*2/10;
            while (true){
                i++;
                if (hBlood<=5||kBlood<=5){
                    break;
                }
                numes = random.nextInt(99)+1;
                if (numes>(100-hBoom)){
                    boom = true;
                }else {
                    boom = false;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (i==2){
                    System.out.println(hName+"使用了"+hSkill+"," +
                            "对"+kName+"造成了"+heroYes);
                    System.out.println(kName+"还剩"+(kBlood-=heroYes));
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(kc.getName()+"使用了普通攻击," +
                            "对"+hc.getName()+"造成了"+kunNo);
                    System.out.println(hc.getName()+"还剩"+(hBlood-=kunNo));
                }else if(i==3){
                    System.out.println(kName+"使用了"+kSkill+"," +
                            "对"+hName+"造成了"+kunYes);
                    System.out.println(hName+"还剩"+(hBlood-=kunYes));
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(hName+"使用了普通攻击," +
                            "对"+kName+"造成了"+heroNo);
                    System.out.println(kName+"还剩"+(kBlood-=heroNo));
                }else if (boom){
                    System.out.println(hName+"使用了暴击攻击," +
                            "对"+kName+"造成了"+heroYes);
                    System.out.println(kName+"还剩"+(kBlood-=heroYes));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(kName+"使用了普通攻击," +
                            "对"+hName+"造成了"+kunNo);
                    System.out.println(hName+"还剩"+(hBlood-=kunNo));
                }else {
                    System.out.println(hName+"使用了普通攻击," +
                            "对"+kName+"造成了"+heroNo);
                    System.out.println(kName+"还剩"+(kBlood-=heroNo));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(kName+"使用了普通攻击," +
                            "对"+hName+"造成了"+kunNo);
                    System.out.println(hName+"还剩"+(hBlood-=kunNo));
                }

            }
            if (hBlood > 0) {
                if (user.getEmprical()>=30){
                    user.setLevel(user.getLevel()+1);
                    user.setEmprical(0);
                }else {
                    user.setEmprical(user.getEmprical()+10);
                }
                userMap.replace(user.getName(),user);
                user.setBlood(user.getLevel()*20+hBlood);
                user.setBoom(user.getLevel()+hBoom);
                user.setDefense(user.getLevel()*3+hDefense);
                user.setPower(user.getLevel()*4+hPower);
                SetUtil.writeObj(userMap,"User.txt");
                return true;
            } else{
                if (user.getEmprical()>=30){
                    user.setLevel(user.getLevel()+1);
                    user.setEmprical(0);
                }else {
                    user.setEmprical(user.getEmprical()+5);
                }
                userMap.replace(user.getName(),user);
                user.setBlood(user.getLevel()*2+hBlood);
                user.setBoom(user.getBlood()+hBoom);
                user.setDefense(user.getLevel()*2+hDefense);
                user.setPower(user.getLevel()*2+hPower);
                SetUtil.writeObj(userMap,"User.txt");
                return false;
            }
        }
    /**
     * 返回剧情关卡
     */
    public Map selectJuQing(){
        return juQingPointMap;
    }
    /**
     * 补给商店买卡
     * @param userName
     * @return
     */
    @Override
    public HeroClass buyCard(String userName) {
        heroClassMap = (Map<String, HeroClass>) SetUtil.readObj("HeroClass.txt");
        if (heroClassMap==null){
            heroClassMap =Init.heroClassMap;
        }
        bagMap = (Map<String, Bag>) SetUtil.readObj("Bag.txt");
        if(bagMap==null){
            bagMap = Init.bagMap;
        }
        System.out.println("奖池：");
        String pass = null;
        String point = null;
        //S级集合
        List<HeroClass> heroListS = new ArrayList<>();
        //A级集合
        List<HeroClass> heroListA = new ArrayList<>();
        //B级集合
        List<HeroClass> heroListB = new ArrayList<>();
        //C级集合
        List<HeroClass> heroListC = new ArrayList<>();
        for (HeroClass heroClass:heroClassMap.values()) {
            pass = heroClass.getName().substring(0,1);
            if (pass.equals("S")){
                heroListS.add(heroClass);
                point = "2%";
            }else if(pass.equals("A")){
                heroListA.add(heroClass);
                point = "10%";
            }else if(pass.equals("B")){
                heroListB.add(heroClass);
                point = "20%";
            }else if(pass.equals("C")){
                heroListC.add(heroClass);
                point = "68%";
            }
            System.out.println(heroClass.getName()+"\t\t"+point);
        }
        if (userMap.get(userName).getkBi()>=20){
            HeroClass hc = null;
            int num = random.nextInt(100)+1;
            int S = random.nextInt(heroListS.size());
            int A = random.nextInt(heroListA.size());
            int B = random.nextInt(heroListB.size());
            int C = random.nextInt(heroListC.size());
            if (num==55||num==32){
                hc = heroListS.get(S);
            }else if(num==30||num==31||num==20||num==12||num==5||num==27||num==85||num==95||num==36||num==88){
                hc = heroListA.get(A);
            } else if(num==2||num==5||num==7||num==96||num==55||num==66||num==82||num==75||num==73||num==25||
                    num==1||num==20||num==17||num==60||num==40||num==51||num==39||num==19||num==49||num==22){
                hc = heroListB.get(B);
            }else {
                hc = heroListC.get(C);
            }
            List<HeroClass> heroes = bagMap.get(userName).getHeroes();
            heroes.add(hc);
            Bag bag = new Bag(userName,heroes);
            bagMap.put(userName,bag);
            userMap.get(userName).setkBi(userMap.get(userName).getkBi()-20);
            SetUtil.writeObj(bagMap,"Bag.txt");
            return hc;
        }else {
            return null;
        }
    }

    public int selectMoney(String userName){
        return userMap.get(userName).getkBi();
    }
    /**
     * 显示背包信息
     * @param cardName
     * @return
     */
    public Bag showHero(String cardName){
        return bagMap.get(cardName);
    }

    /**
     * 充值
     * @param userName
     * @param money
     */
    @Override
    public void recharge(String userName,int money) {
        userMap = (Map<String, User>) SetUtil.readObj("User.txt");
        if(userMap==null){
            userMap = Init.userMap;
        }
        if (userMap.containsKey(userName)){
            userMap.get(userName).setkBi(userMap.get(userName).getkBi()+money);
        }
        SetUtil.writeObj(userMap,"User.txt");
    }

    /**
     * 修改玩家所佩戴的卡片
     * @param userName
     * @param heroClass
     */
    @Override
    public User updateCard(String userName,HeroClass heroClass) {
        User user = userMap.get(userName);
        user.setHero(heroClass);
        userMap.replace(userName,user);
        SetUtil.writeObj(userMap,"User.txt");
        return user;
    }
    /**
     * 删除卡片
     * @param cardName
     */
    @Override
    public User deleteCard(String userName,int cardName) {
        bagMap = (Map<String, Bag>) SetUtil.readObj("Bag.txt");
        if(bagMap==null){
            bagMap = Init.bagMap;
        }
        List<HeroClass> heroes = bagMap.get(userName).getHeroes();
        heroes.remove(cardName);
        Bag bag = new Bag(userName,heroes);
        bagMap.replace(userName,bag);
        if (userMap.containsKey(userName)){
            userMap.get(userName).setkBi(userMap.get(userName).getkBi()+10);
        }
        SetUtil.writeObj(bagMap,"Bag.txt");
        return userMap.get(userName);
    }
    /**
     * 根据id返回玩家对象
     * @param uid
     * @return
     */
    public User print(String uid){
        return userMap.get(uid);
    }

    /**
     * 返回该玩家的背包
     * @param user
     * @return
     */
    public Bag print(User user){
        return bagMap.get(user);
    }
}
